/* 
 * @Author: 朱俊睿
 * @Date:   2016-12-16 14:22:56
 * @Last Modified by:   anchen
 * @Last Modified time: 2017-03-31 11:22:45
 */

function page(opt){
    var obj = opt.obj ? opt.obj : obj;
    var currPage = opt.currPage ? opt.currPage : 1;
    var pageNum = opt.pageNum ? opt.pageNum :5;
    var callback = opt.callback ? opt.callback : function(){};
    obj.innerHTML = '';
    /*if(currPage >=4 && pageNum >=6){
     var oA=document.createElement("a");
     oA.href='#'+1;
     oA.innerHTML = '首页';
     obj.appendChild(oA);
     }*/
    if(currPage >=2){
        var oA=document.createElement("a");
        oA.href='#'+(currPage - 1);
        oA.innerHTML = '&lt;';
        oA.setAttribute('class','arrows');
        oA.setAttribute('id','prev');
        obj.appendChild(oA);
    }
    if(pageNum <= 5){
        for(var i=1 ; i<=pageNum; i++){
            var oA=document.createElement("a");
            if(currPage == i){
                oA.setAttribute('class','active');
            }else{
                oA.setAttribute('class',' ');
            }
            oA.innerHTML= i;
            oA.href='#'+i;
            obj.appendChild(oA);
        }
    }else{
        if(currPage==1 || currPage==2){
            for(var i=1 ; i<=5 ; i++){
                var oA=document.createElement("a");
                if( currPage == i){
                    oA.setAttribute('class','active');
                }else{
                    oA.setAttribute('class',' ');
                }
                oA.innerHTML= i;
                oA.href='#'+i;
                obj.appendChild(oA);
            }
        }
        else if((pageNum-currPage)==0 || (pageNum-currPage)==1){
            for(var i=1 ; i<=5 ; i++){
                var oA=document.createElement("a");
                if( (pageNum-currPage)==0 && i==5 || (pageNum-currPage)==1 && i==4){
                    oA.setAttribute('class','active');
                }else{
                    oA.setAttribute('class',' ');
                }
                oA.innerHTML= pageNum-5+i;
                oA.href='#'+(pageNum-5+i);
                obj.appendChild(oA);
            }
        }
        else{
            for(var i=1 ; i<=5 ; i++){
                var oA=document.createElement("a");
                if( i == 3){
                    oA.setAttribute('class','active');
                }else{
                    oA.setAttribute('class',' ');
                }
                oA.innerHTML= currPage-3+i;
                oA.href='#'+(currPage-3+i);
                obj.appendChild(oA);
            }
        }

    }
    if((pageNum - currPage ) >=1){
        var oA=document.createElement("a");
        oA.href='#'+(currPage - 1 + 2);
        oA.innerHTML = '&gt;';
        oA.setAttribute('class','arrows');
        oA.setAttribute('id','next');
        obj.appendChild(oA);
    }
    callback(currPage,pageNum);
    var aA=obj.getElementsByTagName("a");
    for( var i=0;i<aA.length; i++){
        aA[i].onclick=function(){
            obj.innerHTML="";
            currPage=this.getAttribute("href").substring(1);
            page({
                obj : obj,
                currPage : currPage,
                pageNum : pageNum,
                callback : callback
            });
            // $("html,body").animate({scrollTop:0},500);
            return false;
        }
    }
}